package com.zyl.musicdemo.adapter;


import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zyl.musicdemo.R;
import com.zyl.musicdemo.activitys.PlayMusicActivity;
import com.zyl.musicdemo.bean.MusicModel;

import java.util.ArrayList;
import java.util.List;

public class SongAdapter extends RecyclerView.Adapter<SongAdapter.SongItemViewHolder> {
    public static String SONG_BEAN = "SongBean";
    List<MusicModel> songList = new ArrayList();
    RecyclerView recyclerView;
    boolean isHeight;//用来判断是否是第一次设置高度
    View mView;
    Context mContext;

    public SongAdapter(List<MusicModel> songList, RecyclerView recyclerView) {
        this.songList = songList;
        this.recyclerView = recyclerView;
    }

    @NonNull
    @Override
    public SongItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.song_item, parent, false);
        SongItemViewHolder viewHolder = new SongItemViewHolder(mView);
        mContext = parent.getContext();
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull SongItemViewHolder holder, int position) {
        setRecyclerViewHeight();

        final MusicModel musicModel = songList.get(position);
        Glide.with(holder.itemView)
                .load(musicModel.getPoster())
                .into(holder.ivImage);
        holder.tvMusicName.setText(musicModel.getName());
        holder.tvAuthor.setText(musicModel.getAuthor());


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //播放
                playMusic(musicModel);
            }
        });
        holder.ivPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击播放按钮
                playMusic(musicModel);
            }
        });
    }

    private void playMusic(MusicModel musicModel) {
        Intent intent = new Intent(mContext, PlayMusicActivity.class);
        intent.putExtra(SONG_BEAN, musicModel);
        mContext.startActivity(intent);
    }

    @Override
    public int getItemCount() {
        return songList.size();
    }

    class SongItemViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImage, ivPlay;
        TextView tvMusicName, tvAuthor;

        public SongItemViewHolder(@NonNull View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_image);
            ivPlay = itemView.findViewById(R.id.iv_play);
            tvAuthor = itemView.findViewById(R.id.tv_author);
            tvMusicName = itemView.findViewById(R.id.tv_music_name);
        }
    }


    private void setRecyclerViewHeight() {
        if (isHeight || recyclerView == null) return; //不设置高度
        isHeight = true;
        //多少个单元
        int itemCount = getItemCount();

        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) mView.getLayoutParams();
        //总高度
        int height = layoutParams.height * itemCount;
        Log.d("myTAG", "setRecyclerViewHeight: layoutParams.height：" + layoutParams.height);
        Log.d("myTAG", "setRecyclerViewHeight: height：" + height);

        LinearLayout.LayoutParams recyclerViewParame = (LinearLayout.LayoutParams) recyclerView.getLayoutParams();

        recyclerViewParame.height = height;
        //设置回去
        recyclerView.setLayoutParams(recyclerViewParame);
    }

}
